package com.webchat.client.payment.controller;


import com.webchat.client.payment.service.RedPacketService;
import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.config.annotation.SafeClick;
import com.webchat.common.enums.ClickEvent;
import com.webchat.common.helper.SessionHelper;
import com.webchat.domain.vo.request.SendRedPacketRequestVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/client-service/chat/red-packet")
public class RedPacketController {


    @Autowired
    private RedPacketService redPacketService;

    /**
     * 发红包API
     *
     * @param sendRedPacketRequest
     *
     * @SafeClick 是我们的自定义注解，作用：安全点击，实现原理：基于redis分布式锁
     * SafeClick具体实现详见拦截器：com.webchat.client.chat.config.interceptor.SafeClickInterceptor
     * @return
     */
    @SafeClick(event = ClickEvent.SEND_RED_PACKET, time = 5000, message = "5秒内只能发一次红包")
    @PostMapping("/send")
    public APIResponseBean<Long> send(@RequestBody SendRedPacketRequestVO sendRedPacketRequest) {

        // 发红包参数校验
        sendRedPacketRequest.validateRequestParam();
        // 设置当前登录用户为红包发送人
        String userId = SessionHelper.getCurrentUserId();
        sendRedPacketRequest.setSender(userId);
        Long redPacketId = redPacketService.sendRedPacket(sendRedPacketRequest);
        return APIResponseBeanUtil.success(redPacketId);
    }

    /**
     * 拆分红包
     *
     * 包含群聊多人随机红包拆分 + 一对一固定红包拆分
     *
     * @param redPacketId
     * @return
     */
    @SafeClick(event = ClickEvent.OPEN_RED_PACKET, time = 1000, message = "你已拆分过红包")
    @GetMapping("/open/{redPacketId}")
    public APIResponseBean<String> open(@PathVariable Long redPacketId) {
        String userId = SessionHelper.getCurrentUserId();
        String money = redPacketService.openRedPacket(redPacketId, userId);
        return APIResponseBeanUtil.success(money);
    }
}
